package com.live.dengdengtv.fragment;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.live.dengdengtv.adapter.ConversationAdpter;
import com.tencent.TIMConversation;
import com.tencent.TIMConversationType;
import com.tencent.TIMManager;
import com.tencent.TIMMessage;
import com.tencent.TIMValueCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wyf on 2016/9/22.
 */
public class StrangerMsgFrag extends BaseFragment{

    private static final String TAG = "StrangerMsgFrag";
    private ListView listView;
    private List<TIMMessage> conversationList = new ArrayList<>();
    private BaseAdapter adapter;
    private ConversationItemClick onItemClickListener;

    public void setOnItemClickListener(ConversationItemClick onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if(listView == null) {
            listView = new ListView(getActivity());
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            listView.setLayoutParams(lp);
            listView.setDivider(new ColorDrawable(0xFFE5E5E5));
            listView.setDividerHeight(2);

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    onItemClickListener.onConversationItemClick(conversationList.get(position).getConversation().getPeer());
                }
            });

            adapter = new ConversationAdpter(getActivity(), conversationList);
            listView.setAdapter(adapter);

            getConversation();
        }
        return listView;
    }
    @Override
    public void onDetach() {
        super.onDetach();
        onItemClickListener = null;
    }
    private void getConversation(){
        List<TIMConversation> list = new ArrayList<>();
        //获取会话个数
        long cnt = TIMManager.getInstance().getConversationCount();
        Log.d(TAG, "get " + cnt + " conversations");
        //遍历会话列表
        for(long i = 0; i < cnt; ++i) {
            //根据索引获取会话
            final TIMConversation conversation = TIMManager.getInstance().getConversationByIndex(i);
            if (conversation.getType() == TIMConversationType.System) continue;
            if (conversation.getType() == TIMConversationType.Group) continue;
            list.add(conversation);
            conversation.getMessage(1, null, new TIMValueCallBack<List<TIMMessage>>() {
                @Override
                public void onError(int i, String s) {
                    Log.e(TAG,"get message error"+s);
                }

                @Override
                public void onSuccess(List<TIMMessage> timMessages) {
                    if (timMessages.size() > 0){
                        addMessage(timMessages.get(0));
                        adapter.notifyDataSetChanged();
                    }
                }
            });
        }
    }

    private void addMessage(TIMMessage timMessage) {
        if(timMessage.getConversation().getType() == TIMConversationType.C2C){
            conversationList.add(timMessage);
        }
    }


}
